package net.shopxx.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import chinapnr.SecureLink;

public class PayUtil {

	public static final String PUB_KEY = "PgPubk.key";
	public static final String PRIVATE_KEY = "MerPrK870708.key";
	public static final String PAY_URL = "http://mas.chinapnr.com/gar/RecvMerchant.do";
	public static final String REFUND_URL = "http://mas.chinapnr.com/gao/entry.do";
	public static final String CUR_CODE = "RMB";
	public static final String VERSION = "10";
	public static final String MER_ID = "870708";
	public static final String DEFAULT_CS = "GBK";

	public ThreadLocal<SecureLink> secureLinkLocal = new ThreadLocal<SecureLink>();

	public static int SignMsg(SecureLink secureLink, String keyFile,
			String merData) {
		return secureLink.SignMsg(MER_ID, keyFile, merData);
	}

	public static String getNetConn(String refundUrl, String msg) {
		URL url = null;
		HttpURLConnection httpConn = null;
		StringBuffer resp = new StringBuffer();
		int code = 0;
		try {
			url = new URL(refundUrl);
			httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setConnectTimeout(10000);
			httpConn.setReadTimeout(10000);
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			msg = URLEncoder.encode(msg, DEFAULT_CS);
			httpConn.getOutputStream().write(msg.getBytes(DEFAULT_CS));
			httpConn.getOutputStream().flush();
			httpConn.getOutputStream().close();
			code = httpConn.getResponseCode();
			if (code == 200) {
				// 获取页面内容
				InputStream in = httpConn.getInputStream();
				BufferedReader breader = new BufferedReader(
						new InputStreamReader(in, DEFAULT_CS));
				String str = breader.readLine();
				while (str != null) {
					resp.append(str);
					str = breader.readLine();
				}
			}
			else
			{
				
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		 finally {
			if (httpConn != null)
				httpConn.disconnect();
		}
		return resp.toString();

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
